{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "425ee53e",
   "metadata": {},
   "source": [
    "## 1. APP U本位合约行情-历史K线函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32e60307",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|get_history_candle|获取产品的历史K线数据|\n",
    "|get_history_candle_latest|获取产品指定数量的最新历史K线数据|\n",
    "|get_history_candle_by_date|获取产品指定日期的历史K线数据|\n",
    "|get_history_candle_latest_ts|获取历史K线数据中最新的毫秒时间戳|\n",
    "|update_history_candle|更新产品历史K线数据到指定时间|\n",
    "|candle_to_df|将Binance的candle转化为DataFrame|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "140c75fd",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7075512",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceUM\n",
    "from binance_interface.app.utils import eprint\n",
    "import paux.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2fc55ca7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceUM = BinanceUM(\n",
    "    key=key,\n",
    "    secret=secret,\n",
    "    proxy_host=proxy_host,\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "market = binanceUM.market"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3be1124",
   "metadata": {},
   "source": [
    "## 3. 获取产品的历史K线数据 get_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c56b73f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65861000e+04, 1.65912000e+04, ...,\n",
      "        1.31378000e+02, 2.17938816e+06, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65911000e+04, 1.65915000e+04, ...,\n",
      "        3.88950000e+01, 6.45318694e+05, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65908000e+04, 1.65917000e+04, ...,\n",
      "        2.45070000e+01, 4.06605129e+05, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65482000e+04, 1.65516000e+04, ...,\n",
      "        8.89070000e+01, 1.47141693e+06, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65515000e+04, 1.65531000e+04, ...,\n",
      "        9.65020000e+01, 1.59733195e+06, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65531000e+04, 1.65531000e+04, ...,\n",
      "        7.00480000e+01, 1.15951155e+06, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle(\n",
    "    symbol='BTCUSDT',\n",
    "    start='2023-01-01 00:00:00',\n",
    "    end='2023-01-01 23:59:00',\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a8e3c8c",
   "metadata": {},
   "source": [
    "## 4. 获取产品指定数量的最新历史K线数据 get_history_candle_latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d0981b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.70608242e+12, 3.99616000e+04, 3.99694000e+04, ...,\n",
      "        5.67480000e+01, 2.26762099e+06, 0.00000000e+00],\n",
      "       [1.70608248e+12, 3.99559000e+04, 3.99635000e+04, ...,\n",
      "        3.34760000e+01, 1.33757859e+06, 0.00000000e+00],\n",
      "       [1.70608254e+12, 3.99531000e+04, 3.99567000e+04, ...,\n",
      "        4.28610000e+01, 1.71192235e+06, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.70611824e+12, 4.01692000e+04, 4.01753000e+04, ...,\n",
      "        2.45880000e+01, 9.87725087e+05, 0.00000000e+00],\n",
      "       [1.70611830e+12, 4.01732000e+04, 4.01733000e+04, ...,\n",
      "        2.76780000e+01, 1.11115749e+06, 0.00000000e+00],\n",
      "       [1.70611836e+12, 4.01372000e+04, 4.01523000e+04, ...,\n",
      "        5.27290000e+01, 2.11668553e+06, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_latest(\n",
    "    symbol='BTCUSDT',\n",
    "    length=600,\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c582a71",
   "metadata": {},
   "source": [
    "## 5. 获取产品指定日期的历史K线数据 get_history_candle_by_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bc3050de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65861000e+04, 1.65912000e+04, ...,\n",
      "        1.31378000e+02, 2.17938816e+06, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65911000e+04, 1.65915000e+04, ...,\n",
      "        3.88950000e+01, 6.45318694e+05, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65908000e+04, 1.65917000e+04, ...,\n",
      "        2.45070000e+01, 4.06605129e+05, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65482000e+04, 1.65516000e+04, ...,\n",
      "        8.89070000e+01, 1.47141693e+06, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65515000e+04, 1.65531000e+04, ...,\n",
      "        9.65020000e+01, 1.59733195e+06, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65531000e+04, 1.65531000e+04, ...,\n",
      "        7.00480000e+01, 1.15951155e+06, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_by_date(\n",
    "    symbol='BTCUSDT',\n",
    "    date='2023-01-01',  # 默认时区为美国时区\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eefe3326",
   "metadata": {},
   "source": [
    "## 6. 获取历史K线数据中最新的毫秒时间戳 get_history_candle_latest_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "36822f68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 1706118360000.0, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "ts_result = market.get_history_candle_latest_ts(\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(ts_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daee03ff",
   "metadata": {},
   "source": [
    "## 7. 更新产品历史K线数据到指定时间 update_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b9cdf392",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取candle1，待更新\n",
    "candle1 = market.get_history_candle(\n",
    "    symbol='BTCUSDT',\n",
    "    start='2023-01-01 10:00:00',\n",
    "    end='2023-01-02 10:00:00',\n",
    "    bar='1m',\n",
    ")['data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "11b37457",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67258880e+12, 1.65530000e+04, 1.65531000e+04, ...,\n",
      "        5.05910000e+01, 8.37437882e+05, 0.00000000e+00],\n",
      "       [1.67258886e+12, 1.65530000e+04, 1.65587000e+04, ...,\n",
      "        1.74474000e+02, 2.88856072e+06, 0.00000000e+00],\n",
      "       [1.67258892e+12, 1.65586000e+04, 1.65790000e+04, ...,\n",
      "        1.22173400e+03, 2.02402964e+07, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67267502e+12, 1.67316000e+04, 1.67316000e+04, ...,\n",
      "        1.36640000e+01, 2.28579451e+05, 0.00000000e+00],\n",
      "       [1.67267508e+12, 1.67271000e+04, 1.67272000e+04, ...,\n",
      "        2.32050000e+01, 3.88086302e+05, 0.00000000e+00],\n",
      "       [1.67267514e+12, 1.67239000e+04, 1.67292000e+04, ...,\n",
      "        2.90581000e+02, 4.86002807e+06, 0.00000000e+00]]),\n",
      " 'msg': ''}\n",
      "历史K线时间起点： 2023-01-02 00:00:00\n",
      "历史K线时间终点： 2023-01-02 23:59:00\n",
      "历史K线时间长度： 1440\n"
     ]
    }
   ],
   "source": [
    "# 更新candle1到指定日期时间(智能节约权重)\n",
    "candle_result = market.update_history_candle(\n",
    "    candle=candle1,  # 支持candle1为空\n",
    "    symbol='BTCUSDT',\n",
    "    length=1440,  # 保留数量\n",
    "    end='2023-01-02 23:59:00',  # end默认为本地计算机时间戳\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)\n",
    "\n",
    "candle_start = paux.date.to_fmt(\n",
    "    candle_result['data'][0, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_end = paux.date.to_fmt(\n",
    "    candle_result['data'][-1, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_length = candle_result['data'].shape[0]\n",
    "\n",
    "print('历史K线时间起点：', candle_start)\n",
    "print('历史K线时间终点：', candle_end)\n",
    "print('历史K线时间长度：', candle_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a53625d",
   "metadata": {},
   "source": [
    "## 8. 将Binance的candle转化为DataFrame candle_to_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f9b7b72f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.67258880e+12, 1.65530000e+04, 1.65531000e+04, ...,\n",
       "        5.05910000e+01, 8.37437882e+05, 0.00000000e+00],\n",
       "       [1.67258886e+12, 1.65530000e+04, 1.65587000e+04, ...,\n",
       "        1.74474000e+02, 2.88856072e+06, 0.00000000e+00],\n",
       "       [1.67258892e+12, 1.65586000e+04, 1.65790000e+04, ...,\n",
       "        1.22173400e+03, 2.02402964e+07, 0.00000000e+00],\n",
       "       ...,\n",
       "       [1.67267502e+12, 1.67316000e+04, 1.67316000e+04, ...,\n",
       "        1.36640000e+01, 2.28579451e+05, 0.00000000e+00],\n",
       "       [1.67267508e+12, 1.67271000e+04, 1.67272000e+04, ...,\n",
       "        2.32050000e+01, 3.88086302e+05, 0.00000000e+00],\n",
       "       [1.67267514e+12, 1.67239000e+04, 1.67292000e+04, ...,\n",
       "        2.90581000e+02, 4.86002807e+06, 0.00000000e+00]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candle = candle_result['data']\n",
    "candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "83852e61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>openTs</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>closeTs</th>\n",
       "      <th>turnover</th>\n",
       "      <th>tradeNum</th>\n",
       "      <th>buyVolume</th>\n",
       "      <th>buyTurnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-01-02 00:00:00</td>\n",
       "      <td>16553.0</td>\n",
       "      <td>16553.1</td>\n",
       "      <td>16553.0</td>\n",
       "      <td>16553.1</td>\n",
       "      <td>119.036</td>\n",
       "      <td>2023-01-02 00:00:59</td>\n",
       "      <td>1.970408e+06</td>\n",
       "      <td>728.0</td>\n",
       "      <td>50.591</td>\n",
       "      <td>8.374379e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-01-02 00:01:00</td>\n",
       "      <td>16553.0</td>\n",
       "      <td>16558.7</td>\n",
       "      <td>16553.0</td>\n",
       "      <td>16558.7</td>\n",
       "      <td>200.844</td>\n",
       "      <td>2023-01-02 00:01:59</td>\n",
       "      <td>3.325074e+06</td>\n",
       "      <td>1129.0</td>\n",
       "      <td>174.474</td>\n",
       "      <td>2.888561e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-01-02 00:02:00</td>\n",
       "      <td>16558.6</td>\n",
       "      <td>16579.0</td>\n",
       "      <td>16558.6</td>\n",
       "      <td>16570.0</td>\n",
       "      <td>1639.583</td>\n",
       "      <td>2023-01-02 00:02:59</td>\n",
       "      <td>2.716383e+07</td>\n",
       "      <td>5767.0</td>\n",
       "      <td>1221.734</td>\n",
       "      <td>2.024030e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-01-02 00:03:00</td>\n",
       "      <td>16570.0</td>\n",
       "      <td>16570.1</td>\n",
       "      <td>16559.7</td>\n",
       "      <td>16559.7</td>\n",
       "      <td>510.344</td>\n",
       "      <td>2023-01-02 00:03:59</td>\n",
       "      <td>8.452968e+06</td>\n",
       "      <td>1821.0</td>\n",
       "      <td>192.804</td>\n",
       "      <td>3.193394e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-01-02 00:04:00</td>\n",
       "      <td>16559.7</td>\n",
       "      <td>16559.8</td>\n",
       "      <td>16555.2</td>\n",
       "      <td>16555.3</td>\n",
       "      <td>153.554</td>\n",
       "      <td>2023-01-02 00:04:59</td>\n",
       "      <td>2.542524e+06</td>\n",
       "      <td>723.0</td>\n",
       "      <td>17.379</td>\n",
       "      <td>2.877549e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                openTs     open     high      low    close    volume  \\\n",
       "0  2023-01-02 00:00:00  16553.0  16553.1  16553.0  16553.1   119.036   \n",
       "1  2023-01-02 00:01:00  16553.0  16558.7  16553.0  16558.7   200.844   \n",
       "2  2023-01-02 00:02:00  16558.6  16579.0  16558.6  16570.0  1639.583   \n",
       "3  2023-01-02 00:03:00  16570.0  16570.1  16559.7  16559.7   510.344   \n",
       "4  2023-01-02 00:04:00  16559.7  16559.8  16555.2  16555.3   153.554   \n",
       "\n",
       "               closeTs      turnover  tradeNum  buyVolume   buyTurnover  \n",
       "0  2023-01-02 00:00:59  1.970408e+06     728.0     50.591  8.374379e+05  \n",
       "1  2023-01-02 00:01:59  3.325074e+06    1129.0    174.474  2.888561e+06  \n",
       "2  2023-01-02 00:02:59  2.716383e+07    5767.0   1221.734  2.024030e+07  \n",
       "3  2023-01-02 00:03:59  8.452968e+06    1821.0    192.804  3.193394e+06  \n",
       "4  2023-01-02 00:04:59  2.542524e+06     723.0     17.379  2.877549e+05  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = market.candle_to_df(candle)\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
